package com.aier.admin.service.impl;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.AdminUser;
import com.aier.admin.entity.mapper.AdminUserRowMapper;
import com.aier.admin.service.IAdminUserService;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.entity.Proxy;
import com.aier.proxy.entity.mapper.ProxyRowMapper;
import com.aier.proxy.service.IProxyService;
import com.aier.user.entity.User;
import com.aier.user.entity.mapper.UserRowMapper;

@Component("adminUserService")
public class AdminUserServiceImpl_J implements IAdminUserService {

    @Resource(name="commonDao")
    private ICommonDAO commonDao;
    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    @Override
    public AdminUser searchAdminUser(String account) {
        return (AdminUser)commonDao.listByObject("select * from  admin_user au  left join `user` u  on au.uid=u.uid " +
                "where u.account=? ",new Object[]{account}, new AdminUserRowMapper(AdminUserRowMapper.ADMINUSER_AND_USER));
    }
    /**
     * 查询用户是否存在
     */
    @Override
    public User searchUserAndUserInfo(String account) {
        return (User) commonDao.listByObject("select * from user  " +
                "where account=?", new Object[] { account },new UserRowMapper());
    }
    
    /**
     * 查询城市下的代理商
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Proxy> searchProxyCity(Integer cid) {
        return (List<Proxy>)commonDao.list("select * from proxy p left join proxy_city pc " +
                "on p.pid=pc.pid where pc.cid=?", new Object[]{cid}, new ProxyRowMapper());
    }
    @Override
    public AdminUser searchAdminUser(Long uid) {
        return (AdminUser) commonDao.listByObject("select * from admin_user where uid=?", new Object[]{uid}, new AdminUserRowMapper());
    }
    
    /**
     * 查询管理员列表
     */
    @Override
    public Map<String, Object> searchAdminUserList(int page, int page_size, int sortable) {
        
        String sql = "select u.* from admin_user au left join user u on au.uid = u.uid";
        
        switch (sortable) {
            case IProxyService.ENTRANCE_SORTABLE_SN:
                sql += " order by u.uid";
                break;
            case IProxyService.ENTRANCE_SORTABLE_SN_DESC:
                sql += " order by u.uid desc";
                break;
        }
        return commonDao.queryPage(sql,
                    page, page_size, new Object[] {},
                     new UserRowMapper());
    }
    
    /**
     * 修改管理员密码
     */
    @Override
    public int updatePwd(Long uid, String password) {
        if (commonDao
                .update("update user set pwd=? where uid=?",
                        new Object[] {password,uid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    /**
     * 创建管理员
     */
    @Override
    public int insertUser(String account, String password, String realname,
            String nickname, Short sex, Date birthday) {
        User user = (User) commonDao.listByObject("select * from user where account = ?", new Object[]{account},new UserRowMapper());
        if (null == user) {
         // 保存帐号和密码，并生成uid
            User u = new User(account, password, nickname, null);
            u.setBirthday(birthday);
            u.setRealname(realname);
            u.setIsbaby((short) 1);
            if (sex.equals(1)) {
                u.setSex("男");
            } else {
                u.setSex("女");
            }
            
            KeyHolder kh = commonDao.insert(
                    UserRowMapper.getPreparedStatementCreator(u),
                    new GeneratedKeyHolder());
            
            
            // 管理员表中插入记录
            commonDao.update("insert into admin_user(uid) values(?)",new Object[]{kh.getKey().longValue()});
        } else {
            // 管理员表中插入记录
            commonDao.update("insert into admin_user(uid) values(?)",new Object[]{user.getUid()});
        }
        
        return 1;
    }    


}
